<?php

class AlbumController extends BaseController
{

	/**
	 * 新建图片
	 */
	public function actionCreate()
	{
		$model= Albums::model()->findByAttributes(array('zat_id'=>$_POST['Albums']['zat_id']));

		if(isset($_POST))
		{
			$this->redirect(array('admin'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 编辑图片
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{

		$model=$this->loadModel($id);
	//	print_r($model->attributes);exit;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Albums']))
		{
			$model->attributes=$_POST['Albums'];
			if($model->save()){
				$this->redirect(array('admin'));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除图片
	 */
	public function actionDelete()
	{
		$id = isset($_GET['id'])?$_GET['id']:$_POST['ids'];
		foreach((array)$id as $val){
			$criteria=new CDbCriteria;
			$criteria->compare('zta_id',$val);
			//检查图片是否被产品使用，如有使用不能删除
			if($proimg=ProImages::model()->find($criteria)){
				
				$product=Product::model()->findByPk($proimg->zp_id);
				$this->render('../index/error',
							  array(
								'message'=>'对不起，操作失败，图片已被商品'.$product->zp_name.'使用,不能删除！',
								'links'=>array(array('返回图片管理',Yii::app()->homeUrl.'/manage/album/admin')),
							  )
				);
				
				exit;
			}
			//相册图片记录
			$albums = Albums::model()->findByPk($val);
			//附件记录
			$attachment = Attachment::model()->findByPk($albums->zat_id);
			//物理删除图片
			Tool::delUpload($attachment->zat_url);
			//删除图片记录
			$albums->delete();
			//删除附件记录
			$attachment->delete();
		}
		//成功跳转
		$this->redirect(Yii::app()->homeUrl.'/manage/album/admin');
	}

	/**
	 * 查看图片
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 图片管理
	 */
	public function actionAdmin()
	{
		$model=new Albums('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Albums']))
			$model->attributes=$_GET['Albums'];
		
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 载入模型
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Albums::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * ajax验证
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-cat-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 新建相册分类
	 */
	public function actionCreateAlbum()
	{
		$model=new CmpCat;
		
		if(isset($_GET['zpc_catname']))
		{
			$catname = trim($_GET['zpc_catname']);
			$prentid = trim($_GET['prentid']);

			//判断父级分类下面有没有图片，把图片分类设为0
			$zmpPro = Albums::model()->findByAttributes(array('zpc_id'=>$prentid));
			if(isset($zmpPro->zpc_id)){
				Albums::model()->updateAll(array('zpc_id'=>'0','zpc_id=:zpc_id',array(':zpc_id'=>$prentid)));
			}
			//查询顶级分类信息
			$prentcatinfo = CmpCat::model()->findByPk($prentid);
			$topid = 0;
			$childid = null;
			if(!empty($prentcatinfo)){
				$topid   = $prentcatinfo->zpc_parentid;
				$childid = $prentcatinfo->zpc_childid;
			}
			$model->zpc_catname = $catname;
			$model->zpc_parentid = $prentid;
			$model->zpc_topid = $topid;
			$model->zpc_type = 3;//类型：3相册分类
			$model->zm_id = Company::model()->getCmpUserId();
			$model->save();
			//修改父级分类的zpc_childid
			if(!empty($prentcatinfo)){
				if(isset($childid)){
					$prentcatinfo->zpc_childid = $childid.','.$model->zpc_id;
				}else{
					$prentcatinfo->zpc_childid =$model->zpc_id;
				}
				$model->save();
			}
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
			exit;
		}else{
			throw new CHttpException("参数出错");
		}
	
	}
	
	/**
	 * 保存操作
	 */
	public function actionSave(){
		$catname = $_POST['data']['zpc_catname'];
		$newcatname = array_unique($_POST['data']['zpc_catname']);
		$trimcatname = array_filter($_POST['data']['zpc_catname']);
		//判断分类是否存在重复
		if(count($catname) != count($newcatname)){
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，分类名称不能有重复！'));
			exit;
		}
		//判断分类名称是否存在空值
		if(count($catname) != count($trimcatname)){
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，分类名称不能为空！'));
			exit;
		}
		foreach ($_POST['data']['zpc_id'] as $key => $val){
			$model = CmpCat::model()->findByPk($val);
			$model->zpc_catname   = trim($_POST['data']['zpc_catname'][$key]);
			$model->zpc_pic       = $_POST['data']['zpc_pic'][$key];
			$model->zpc_listorder = $key;
			$model->save();
		}
		//成功跳转
		$this->redirect(Yii::app()->request->urlReferrer);
	}
	
	/**
	 * 删除相册
	 */
	public function actionDeleteAlbum($id)
	{
		$model = CmpCat::model()->findByPk($id);
		//判断该分类下面有没有子分类，有子分类不能进行删除
		if(!isset($model->zpc_childid)){
			//判断该分类下面有没有产品
			$zmpPro = CmpCatlog::model()->findByAttributes(array('zpc_id'=>$id));
			if(isset($zmpPro->zpc_id)){
				//失败跳转
				$this->render('../index/error',array('message'=>'该相册下面有图片，请先删除图片！'));
				exit;
			}
			//先修改父级分类下面的子分类id
			$parentinfo 	= CmpCat::model()->findByPk($model->zpc_parentid);
			if(isset($parentinfo->zpc_childid)){
				$childid 		= explode(',',$parentinfo->zpc_childid);
				unset($childid[array_search($id,$childid)]);
				$parentinfo->zpc_childid = implode(',',$childid);
				$parentinfo->save();
			}
			if($model->delete()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'请确保该相册下面没有子相册'));
			exit;
		}
	}
	
	/**
	 * 转移到相册
	 */
	public function actionRemoveToAlbum(){

		$catid = (int)($_POST['cat_id']);
		$ids = $_POST['ids'];
		
		$criteria=new CDbCriteria;
		$criteria->compare('zal_id',$ids);
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		if(Albums::model()->updateAll(array('zpc_id'=>$catid),$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
}
